/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.Vista;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.BCNTours.R;
import com.BCNTours.DataBase.DataBaseHelper;
import com.BCNTours.Model.ApplicationData;

/**
 * Screen that shows while the application loads while its opening.
 *
 */
public class SplashScreenActivity extends Activity{
    private ApplicationData applicationData;
    public DataBaseHelper dataBase;
    public SQLiteDatabase db;
    private int lang = 3;
    
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.splash);
		/** set time to splash out */
		@SuppressWarnings("unused")
		final int welcomeScreenDisplay = 5000;
		
		/** create a thread to show splash up to splash time */
		Thread welcomeThread = new Thread() {
			@SuppressWarnings("unused")
			int wait = 0;
		
			@Override
			public void run() {
				try {
					super.run();
					/* Loading the preferences */
					Context context = getApplicationContext(); 
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 	//obtains the preferences
					
					lang= prefs.getInt(Preferences.PREF_LANG, 0); 										//obtains the selected language 
					
					/* Loading the database */
					dataBase = new DataBaseHelper(getApplicationContext(), null, null, 1);
			        db = dataBase.getWritableDatabase();
			        
			        applicationData = new ApplicationData();
			        applicationData.setLang(dataBase.getLanguaje(db));
			        applicationData.setLists(dataBase.getCompletList(db, lang));
			        
			        /* Close the database */
			        db.close();
			        dataBase.close();
			        
			       
			        
				} catch (Exception e) {
					Log.e("error","EXc=" + e);
					if(lang == 0){
						Intent i = new Intent(SplashScreenActivity.this, LanguageSetting.class);
						startActivity(i);
						db.close();
						dataBase.close();
					}
				} finally {
				   /**
					* Called after splash times up. Do some action after splash
					* times up. Here we moved to another main activity class
					*/
					if(lang != 0){
						String languageToLoad;
						switch(lang){
							case 1:
								languageToLoad = "es";
								break;
							case 2:
								languageToLoad = "ca";
								break;
							case 3:
								languageToLoad = "en";
								break;	
							default:
								languageToLoad = "en";
								break;		
						}
						Log.i("language","languageToLoad");
					    Locale locale = new Locale(languageToLoad);
					    Locale.setDefault(locale);
					    Resources r = getResources();
					    r.finishPreloading();
					    Configuration config = new Configuration();
					    config.locale = locale;
					    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

						Intent i = new Intent(SplashScreenActivity.this, BCNTours.class);
						i.putExtra("database", applicationData);
						startActivity(i);	
					}
					finish();
				}
			}
		};
		welcomeThread.start();
	}
}
